#include "./logic.h"
#include "./getSystemInfo.h"
#include "./login.h"

const char magic[4] = {'C', 'r', 't', 'l'};


void TlogicManage::processRecvPackge(protocol::TprotocolBaseClass* avProtocol, protocol::TmetaData* avData)
{
	TMyMetaData* lvMyMetaData = static_cast<TMyMetaData*>(avData);	
	if (memcmp(lvMyMetaData->head()->magic, magic, sizeof(magic)) == 0)
	{
		avProtocol->setClose();
		return;
	}
}

void TlogicManage::sendOnlineData(protocol::TprotocolBaseClass* avProtocol)
{
	getSystemInfo lvSysInfo;
	lvSysInfo.getInfo();

	Logic::Tlogin lvLogin;
	lvLogin.computerName = "abcd";
	lvLogin.ip.assign(lvSysInfo.localIP[0]);
	lvLogin.appId = 1;

	auto lvData = lvLogin.serialize();
	packSendData(avProtocol, EMessageID::online, lvData);
}

void TlogicManage::packSendData(protocol::TprotocolBaseClass* avProtocol, EMessageID avMsgID, std::vector<uint8_t> avSendData)
{
	TMyMetaData lvMyMetaData = {};
	protocol::TprotocolHead lvHead = {};
	lvMyMetaData.headData.insert(lvMyMetaData.headData.begin(), (uint8_t*)&lvHead, (uint8_t*)&lvHead + sizeof(lvHead));
	TExtendHead lvExtendHead = {};
	lvMyMetaData.extendHeadData.insert(lvMyMetaData.extendHeadData.begin(), (uint8_t*)&lvExtendHead, (uint8_t*)&lvExtendHead + sizeof(lvExtendHead));
	lvMyMetaData.payload = std::move(avSendData);

	lvMyMetaData.extendHead()->MsgID = 100;
	memcpy(lvMyMetaData.head()->magic, magic, sizeof(magic));
	lvMyMetaData.head()->dataLen = lvMyMetaData.payload.size();

	avProtocol->putSendData(std::move(lvMyMetaData));
}